﻿using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UMLSynthesizer.CSharpAnalyzer.Elements;
using UMLSynthesizer.CSharpAnalyzer.Helpers;
using UMLSynthesizer.LangAnalyzer.Elements;
using UMLSynthesizer.LangAnalyzer.Expressions;
using UMLSynthesizer.LangAnalyzer.Statements;

namespace UMLSynthesizer.CSharpAnalyzer.Expressions
{
    class CSharpObjectCreationExpression : CSharpExpressionBase, IObjectCreationExpression
    {
        #region Fields

        CSharpExpressionHelper argExprHelper;
        CSharpExpressionHelper initExprHelper;

        #endregion

        #region Constructor

        public CSharpObjectCreationExpression(ObjectCreationExpressionSyntax expressionNode,
            IExpressionBase parentExpression,
            IStatementBase parentStatement)
            : base(expressionNode, parentExpression, parentStatement, ExpressionKinds.ObjectCreation)
        {
            argExprHelper = new CSharpExpressionHelper(this, parentStatement,
                () =>
                {
                    if (expressionNode.ArgumentList == null)
                        return new ExpressionSyntax[0];
                    return from arg in expressionNode.ArgumentList.Arguments
                           select arg.Expression;
                });
            initExprHelper = new CSharpExpressionHelper(this, parentStatement, () => expressionNode.Initializer);
        }

        #endregion

        #region Properties

        public IEnumerable<IExpressionBase> ArgumentList { get { return argExprHelper.Expressions; } }
        public IExpressionBase Initializer { get { return initExprHelper.Expression; } }
        public ITypeBase Type
        {
            get
            {
                return (ProjectOwner as CSharpProjectElement)
                    .GetTypeReference((expressionNode as ObjectCreationExpressionSyntax).Type);
            }
        }
        public IEnumerable<ITypeBase> TypeArgumentList
        {
            get
            {
                if ((expressionNode as ObjectCreationExpressionSyntax).Type as GenericNameSyntax != null)
                    return from argument in ((expressionNode as ObjectCreationExpressionSyntax).Type
                               as GenericNameSyntax).TypeArgumentList.Arguments
                           select (ProjectOwner as CSharpProjectElement).GetTypeReference(argument);
                return new ITypeBase[0];
            }
        }

        #endregion
    }
}
